/**
*玩家类
 */
public class Player {
	//玩家状态
	public static final int WAIT=1;
	public static final int READY=2;
	public static final int THINKING=3;
	public static final int WAIT_OTHER=4;
	public static final int WIN=5;
	
	int id=-1;//玩家ID
	int seatNum;//座位号
	String name;//姓名
	boolean autoPlay=false;//是否自动打牌，托管
	int score=0;//当前分数
	int preScore=0;//上一轮分数
	int state;//玩家状态
	Connection conn;

	private int [] haveCards;//手中的牌
	private int [] disCards;//打的牌
	
	public Player(){
		name="Player"+id;
	}

	public Player(int id){
		this.id=id;
		name="Player"+id;
	}
	
	public void setConn(Connection c){
		this.conn=c;
	}
	
	public void setID(int id){
		this.id=id;
	}
	
	public void setID(String id){
		try{
			this.id=Integer.parseInt(id);
		}catch(Exception e){e.printStackTrace();}		
	}	

	public void playCards(int[] cards){
		String outStr;
		outStr="cmd=playCard&ID="+id+"&cards=";
		for(int i=0;i<cards.length;i++){
			outStr=outStr+cards[i]+",";
		}

	conn.sendMsg(outStr);
}
	
	public void setName(String name){
		if(name!=null) this.name=name;
	}
	
	public void setScore(int score){
		this.score=score;
	}
	
	public void setScore(String score){
		try{
			this.score=Integer.parseInt(score);
		}catch(Exception e){e.printStackTrace();}
	}
	
	public void setState(int state){
		this.state=state;
	}
	
	public void receiveCards(int [] cards){
		int [] newCards;
		if(haveCards!=null){
			newCards=new int[haveCards.length+cards.length];
			System.arraycopy(haveCards,0,newCards,0,haveCards.length);
			System.arraycopy(cards,0,newCards,haveCards.length,cards.length);
		}
		else{
			newCards=new int[cards.length];
			System.arraycopy(cards,0,newCards,0,cards.length);	
		}
		
		java.util.Arrays.sort(newCards);
		haveCards=newCards;
	}
	
	public int cardNum(){
		if(haveCards!=null) return haveCards.length;
		else return 0;
	}
	
	public int removeCards(int [] cards){
		int [] newCards;
		newCards=Rule.removeCards(haveCards,cards);
		if (newCards!=null) haveCards=newCards;
		return haveCards.length;
	}
	
	public void setDisCards(int [] cards){
		disCards=new int[cards.length];
		System.arraycopy(cards,0,disCards,0,cards.length);
	}
	
	public void pass(){
		disCards=new int[1];
		disCards[0]=0;
	}
	
	public int [] getDisCards(){
		int [] newCards;
		if(disCards!=null){
			newCards=new int[disCards.length];
			System.arraycopy(disCards,0,newCards,0,disCards.length);
			java.util.Arrays.sort(newCards);
			return newCards;
		}
		else return null;
	}	
	
	public int [] getCards(){
		int [] newCards;
		if(haveCards!=null){
			newCards=new int[haveCards.length];
			System.arraycopy(haveCards,0,newCards,0,haveCards.length);
			return newCards;
		}
		else return null;	
	}
	
	public void clearDisCards(){
		disCards=null;
		}
		
	public void newGame(){
		state=WAIT;
		haveCards=null;
		disCards=null;
	}
	
}